package rs.daolao.syc.admin.account.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import rs.dalao.syc.common.cache.HttpConsts;
import rs.dalao.syc.common.ex.ServiceException;
import rs.dalao.syc.common.pojo.authentication.CurrentPrincipal;
import rs.dalao.syc.common.pojo.vo.PageData;
import rs.dalao.syc.common.response.JsonResult;

import rs.dalao.syc.common.response.ServiceCode;
import rs.daolao.syc.admin.account.pojo.vo.DoctorListVO;
import rs.daolao.syc.admin.account.pojo.vo.UserListItemVO;
import rs.daolao.syc.admin.account.pojo.vo.UsersSearchVO;
import rs.daolao.syc.admin.account.service.IDoctorService;
import rs.daolao.syc.admin.account.service.IUserService;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 处理用户相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 2.0
 */
@Slf4j
@RestController
@RequestMapping("/users")
@Validated
@Api(tags = "1. 用户管理")
public class UserAdminController implements HttpConsts {

    @Autowired
    private IUserService userService;
    @Autowired
    private IDoctorService iDoctorService;

    public UserAdminController() {
        log.info("创建控制器对象：UserController");
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/admin')")
    @ApiOperation("根据ID删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult deleteUser(@PathVariable Long id) {
        log.debug("开始处理【根据ID删除用户】的请求，参数：{}", id);
        userService.delete(id);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/deletedoc")
    @PreAuthorize("hasAuthority('/admin')")
    @ApiOperation("根据ID删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult deleteDoc(@PathVariable Long id) {
        log.debug("开始处理【根据ID删除医生】的请求，参数：{}", id);
        iDoctorService.deleteDoc(id);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/admin')")
    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 350)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用用户】的请求，参数：{}", id);
        userService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/admin')")
    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 351)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable Long id,@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【禁用用户】的请求，参数：{}", id);
        if (id.equals(currentPrincipal.getId())){
            String message = "操作失败，无法禁用自身账号！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        userService.setDisable(id);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/docenable")
    @PreAuthorize("hasAuthority('/admin')")
    @ApiOperation("启用医生")
    @ApiOperationSupport(order = 352)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult setDocEnable(@PathVariable Long id) {
        log.debug("开始处理【启用用户】的请求，参数：{}", id);
        userService.setDocDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/docdisable")
    @PreAuthorize("hasAuthority('/admin')")
    @ApiOperation("禁用医生")
    @ApiOperationSupport(order = 353)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult setDocDisable(@PathVariable Long id,@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【禁用用户】的请求，参数：{}", id);
        if (id.equals(currentPrincipal.getId())){
            String message = "操作失败，无法禁用自身账号！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        userService.setDocEnable(id);
        return JsonResult.ok();
    }
    @GetMapping("/queryAllUsers")
    @PreAuthorize("hasAuthority('/admin')")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "2", dataType = "long")
    })
    public JsonResult list(Integer page) {
        log.debug("开始处理【查询用户列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<UserListItemVO> pageData = userService.userList(pageNum);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/queryAllDoctors")
    @PreAuthorize("hasAuthority('/admin')")
    @ApiOperation("查询医生列表")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "2", dataType = "long")
    })
    public JsonResult listDoctor(Integer page) {
        log.debug("开始处理【查询医生列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<DoctorListVO> pageData = userService.doctorList(page);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/rebuild-search")
    @PreAuthorize("hasAuthority('/admin')")
    @ApiOperation("重建用户的搜索数据")
    @ApiOperationSupport(order = 422)
    public JsonResult rebuildSearch( ) {
        log.debug("开始处理【重建用户的搜索数据】的请求");
        userService.rebuildSearch();
        return JsonResult.ok();
    }

    @GetMapping("/query")
    @PreAuthorize("hasAuthority('/admin')")
    @ApiOperation("多条件高级搜索")
    public JsonResult customQuery(String title){
        List<UsersSearchVO> encContentSearchVOS = userService.customQueryUser(title);
        return JsonResult.ok(encContentSearchVOS);
    }
    @PostMapping("/logout")
    @ApiOperation("用户退出")
    public JsonResult logout(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        userService.logout(currentPrincipal);
        return JsonResult.ok();
    }
}
